package kuzmenkov.oip.controller.applet;

import java.awt.BorderLayout;
import java.util.logging.Logger;

import javax.swing.JApplet;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;

/**
 * Class initializes the applet
 * 
 * @author Michael Kuzmenkov
 * @date 08.03.2009
 */
public class ImageProcessingApplet extends JApplet {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7460077846558040308L;
	private Logger log = Logger
			.getLogger(ImageProcessingApplet.class.getName());

	// private JFrame frame;
	// private JScrollPane pane;

	/**
	 * An entry point
	 */
	public void init() {
		log.info("entry");
		ClassPathResource resource = new ClassPathResource(
				"applicationContext.xml");

		BeanFactory factory = new XmlBeanFactory(resource);
		final ClientService service = (ClientService) factory
				.getBean("clientService");

		I18nTool itool = I18nTool.getInstance();
		try {
			createGUI(service);
		} catch (Exception e) {
			log.severe("Occurred error: " + e);
			JOptionPane.showMessageDialog(null, e.getMessage(), itool
					.getMessage("errorDialogTitle"), JOptionPane.ERROR_MESSAGE);
			e.printStackTrace();
		}
	}

	/**
	 * Creates the GUI of the image processing applet e.g. this applet
	 * 
	 * @param service
	 *            a service of a remote access to a server
	 * @throws Exception
	 */
	private void createGUI(ClientService service) throws Exception {
		// frame = new JFrame();
		// frame.setVisible(false);
		// frame.setSize((int)getContentPane().getSize().getWidth()+20,
		// (int)getContentPane().getSize().getHeight()+80);
		// frame.setLocationRelativeTo(null);

		JPanel mainPanel = new MainPanel(this, service);
		// mainPanel.setPreferredSize(getContentPane().getSize());
		JScrollPane pane = new JScrollPane(mainPanel);

		// JButton but = new JButton(new ShowFloatFrameL());
		getContentPane().add(pane, BorderLayout.CENTER);
		// getContentPane().add(but, BorderLayout.NORTH);

	}

	// class ShowFloatFrameL extends AbstractAction {
	// public ShowFloatFrameL() {
	// }
	//
	// public void actionPerformed(ActionEvent e) {
	// if (frame.isVisible()){
	// frame.setVisible(false);
	// log.info("1: content pane size="+getContentPane().getSize());
	// frame.getContentPane().remove(0);
	// getContentPane().add(pane,BorderLayout.CENTER);
	//				
	// repaint();
	//				
	// } else {
	// log.info("2: content pane size="+getContentPane().getSize());
	// getContentPane().remove(0);
	//				
	// frame.getContentPane().add(pane);
	// frame.setVisible(true);
	//				
	// repaint();
	// }
	// }
	// }

}
